package com.xueyi.system.BaseCropGrowth.controller;

import com.xueyi.common.security.utils.SecurityUtils;
import com.xueyi.system.BaseCropGrowth.domain.query.BaseCropGrowthQuery;
import com.xueyi.system.BaseCropGrowth.domain.dto.BaseCropGrowthDto;
import com.xueyi.system.BaseCropGrowth.service.IBaseCropGrowthService;
import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.system.BaseMyMsg.domain.dto.BaseMyMsgDto;
import com.xueyi.system.BaseMyMsg.domain.query.BaseMyMsgQuery;
import com.xueyi.system.BaseMyMsg.service.IBaseMyMsgService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * 作物长势管理 业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/growth")
public class BaseCropGrowthController extends BaseController<BaseCropGrowthQuery, BaseCropGrowthDto, IBaseCropGrowthService> {


    @Autowired
    private IBaseCropGrowthService baseCropGrowthService;

    @Autowired
    private IBaseMyMsgService baseMyMsgService;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "作物长势" ;
    }

    /**
     * 查询作物长势列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.BASE_CROP_GROWTH_LIST)
    public AjaxResult list(BaseCropGrowthQuery baseCropGrowth) {
        AjaxResult list = super.list(baseCropGrowth);
        return list;
    }

    /**
     * 查询作物长势详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.BASE_CROP_GROWTH_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {

        String userName = SecurityUtils.getUser().getUserName();
        System.out.println("kjdshkdfjh "+userName);
        BaseMyMsgQuery baseMyMsg = new BaseMyMsgQuery();
        baseMyMsg.setReceiveUser(userName);
        baseMyMsg.setReceiveStatus("2");
        List<BaseMyMsgDto> msgDtoList = baseMyMsgService.selectList(baseMyMsg);
        for (BaseMyMsgDto msgDto : msgDtoList) {
            msgDto.setReceiveStatus("1");
            baseMyMsgService.update(msgDto);
        }

        return super.getInfoExtra(id);
    }

    /**
     * 作物长势导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.BASE_CROP_GROWTH_EXPORT)
    @Log(title = "作物长势管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, BaseCropGrowthQuery baseCropGrowth) {
        super.export(response, baseCropGrowth);
    }

    /**
     * 作物长势新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.BASE_CROP_GROWTH_ADD)
    @Log(title = "作物长势管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody BaseCropGrowthDto baseCropGrowth) {
        return super.add(baseCropGrowth);
    }

    /**
     * 作物长势修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.BASE_CROP_GROWTH_EDIT)
    @Log(title = "作物长势管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody BaseCropGrowthDto baseCropGrowth) {
        return super.edit(baseCropGrowth);
    }

    /**
     * 作物长势批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.BASE_CROP_GROWTH_DEL)
    @Log(title = "作物长势管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 日期范围
     */
    @GetMapping("/findDatelist")
    public AjaxResult findDatelist() {
        List<BaseCropGrowthDto> datelist = baseCropGrowthService.findDatelist();
        return AjaxResult.success(datelist);
    }

    /**
     * 获取作物长势选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - 作物长势管理 - 列表 */
        String BASE_CROP_GROWTH_LIST = "system:growth:list" ;
        /** 系统 - 作物长势管理 - 详情 */
        String BASE_CROP_GROWTH_SINGLE = "system:growth:single" ;
        /** 系统 - 作物长势管理 - 新增 */
        String BASE_CROP_GROWTH_ADD = "system:growth:add" ;
        /** 系统 - 作物长势管理 - 修改 */
        String BASE_CROP_GROWTH_EDIT = "system:growth:edit" ;
        /** 系统 - 作物长势管理 - 删除 */
        String BASE_CROP_GROWTH_DEL = "system:growth:delete" ;
        /** 系统 - 作物长势管理 - 导出 */
        String BASE_CROP_GROWTH_EXPORT = "system:growth:export" ;
    }
}
